package com.hnchances.grade.common.utils;

import cn.hutool.crypto.SecureUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.io.UnsupportedEncodingException;
import java.util.Date;

public class JWTUtils {

    /**
     * 过期时间
     */
    private static final long EXPIRE_TIME = 7 * 24 * 60 * 60 * 1000;


    /**
     * 校验
     * @param token
     * @param username
     * @param password
     * @return
     */
    public static boolean verify(String token, String username, String password) {

        try {
            System.out.println(token);
            System.out.println(username);
//            System.out.println("验证时使用的密码"+password);
            Algorithm algorithm = Algorithm.HMAC256(password);
            JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 1
     * 颁发令牌
     * @param username
     * @param password
     * @return
     */
    public static String sign(String username, String password) {
        try {
            //设置过期时间：获取当前时间+过期时间（毫秒）
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            //设置签名的加密算法：HMAC256
//            System.out.println("生成时使用的密码"+password);
//            Algorithm algorithm = Algorithm.HMAC256(password);
            Algorithm algorithm = Algorithm.HMAC256(SecureUtil.md5(password));
            // 附带username信息
            return JWT.create()
                    .withClaim("username", username)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * 1
     * 获取用户名
     * @param token
     * @return
     */
    public static String getUsername(String token) {
        if (token == null || "".equals(token)) {
            return null;
        }
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String user = "张三666";
        String password = "123456";
        String sign = sign(user, password);
        System.out.println(sign);
        boolean verify = verify(sign, user, "e10adc3949ba59abbe56e057f20f883e");
        System.out.println(verify);
    }


}

